package cn.jjxx.modules.sys.security.shiro.web.filter.authc;

import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.modules.portal.dto.LoginDto;
import sun.misc.BASE64Decoder;

/**
 * All rights Reserved, Designed By www.jjxxkj.cn
 *
 * @version V1.0
 * @title: UsernamePasswordToken.java
 * @package cn.jjxx.modules.sys.security.shiro.web.filter.authc
 * @description: 用户和密码（包含验证码）令牌类
 * @author: www.jjxxkj.cn
 * @date: 2017年6月26日 下午5:56:18
 * @copyright: 2017 www.jjxxkj.cn Inc. All rights reserved.
 */
public class UsernamePasswordToken extends org.apache.shiro.authc.UsernamePasswordToken {

	private static final long serialVersionUID = 1L;

	private String captcha;
	private boolean mobileLogin;
	private String type;
	private Integer loginMode;
	private String userId;
	private boolean ssoLogin = false;

	public boolean isSsoLogin() {
		return ssoLogin;
	}

	public void setSsoLogin(boolean ssoLogin) {
		this.ssoLogin = ssoLogin;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public UsernamePasswordToken() {
		super();
	}

	public UsernamePasswordToken(String username, char[] password, boolean rememberMe, String host, String captcha,
								 boolean mobileLogin) {
		super(username, decryptToChar(password), rememberMe, host);
		this.captcha = captcha;
		this.mobileLogin = mobileLogin;
	}

	/**
	 * 郑成功加入单点登录TOKEN
	 */
	public UsernamePasswordToken(String userId, boolean rememberMe, String host, String captcha, boolean mobileLogin, boolean ssoLogin) {
		//super(null, new String().toCharArray() , rememberMe, host);
		super(null, new String().toCharArray(), rememberMe, host);
		this.userId = userId;
		this.captcha = captcha;
		this.mobileLogin = mobileLogin;
		this.ssoLogin = ssoLogin;
	}

	public UsernamePasswordToken(String username, char[] password, boolean rememberMe, String host, String captcha, boolean mobileLogin, String type) {
		super(username, password, rememberMe, host);
		this.captcha = captcha;
		this.mobileLogin = mobileLogin;
		this.type = type;
	}

	public UsernamePasswordToken(LoginDto loginDto, String host) {
		super(loginDto.getUsername(), loginDto.getPassword(), loginDto.getRememberMe(), host);
		this.captcha = loginDto.getJcaptchaCode();
		this.mobileLogin = loginDto.getMobileLogin();
		this.loginMode = loginDto.getLoginMode();
	}


	public String getCaptcha() {
		return captcha;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	public boolean isMobileLogin() {
		return mobileLogin;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Integer getLoginMode() {
		return loginMode;
	}

	public void setLoginMode(Integer loginMode) {
		this.loginMode = loginMode;
	}

	public static char[] decryptToChar(char[] strArr) {
		String str = "";
		try {
			if (!ObjectUtils.isNullOrEmpty(strArr)) {
				String newStr = new String(strArr);
				str = decode(newStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str.toCharArray();
	}

	/**
	 * 字符串解密，采用BASE64的算法
	 *
	 * @param encoder 需要解密的字符串
	 * @return 解密后的字符串
	 */
	public static final String decode(String encoder) {
		try {
			BASE64Decoder decoder = new BASE64Decoder();
			byte[] buf = decoder.decodeBuffer(encoder);
			return new String(buf,"UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}